import React from 'react';
import ReactDOM from 'react-dom';
import {createStore,applyMiddleware} from 'redux';
import Reducer from './reducer.js'
import thunk from 'redux-thunk'
import $ from 'jquery'
import TodoApp from './components/TodoApp.js'

var store=createStore(Reducer,{list:[],carlist:[]},applyMiddleware(thunk));


//监听页面渲染
store.subscribe(render);
function render(){
	ReactDOM.render(
		<TodoApp getData={getData} carlist={store.getState().carlist} list={store.getState().list} addToCar={addToCar} removeFromCar={removeFromCar}/>,
		document.querySelector('#box')
	)
}

render();

// getData('GET_PRODUCT_DATA');

function getData(postTitle){
	store.dispatch(function(dispatch,getState){
		//首先发出第一个异步请求
		dispatch({
			type:postTitle+"_START"
		})
		$.get('../api/products.json',function(res){
			//console.log(res);
			dispatch({
				type:postTitle+"_END",
				todolist:res
			})
		})
	})
}

function addToCar(action){
	store.dispatch(action)
}

function removeFromCar(action){
	store.dispatch(action)
}
